<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>合同列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/lib/layui/layui.js"
            charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/xadmin.js"></script>
    <script charset="utf-8" language="javascript" type="text/javascript" src="JS/CommonFields/common.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">合同</a>
        <a>
          <cite>列表</cite></a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so" onsubmit="return false;">
            <input type="text" name="contractName" placeholder="请输入合同名称" autocomplete="off" class="layui-input">
            <div class="layui-inline">
                <label class="layui-form-label">类型:</label>
                <div class="layui-input-inline">
                    <select name="contractType" lay-verify="required" lay-search="">
                        <option value="">选择类型</option>
                        <option value="0">销售合同</option>
                        <option value="1">技术合同</option>
                        <option value="2">运输合同</option>
                        <option value="3">委托合同</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">状态:</label>
                <div class="layui-input-inline">
                    <select name="contractStatus" lay-verify="required" lay-search="">
                        <option value="">选择状态</option>
                        <option value="0">已订立</option>
                        <option value="1">已提交</option>
                        <option value="2">待审核</option>
                        <option value="3">已审核</option>
                        <option value="4">履行中</option>
                        <option value="5">已履行</option>
                        <option value="6">已变更</option>
                        <option value="7">已解除</option>
                        <option value="8">已转让</option>
                        <option value="9">已终止</option>
                        <option value="10">已审核</option>
                    </select>
                </div>
            </div>
            <button class="layui-btn" id="search"><i class="layui-icon">&#xe615;</i></button>
        </form>
    </div>
    <xblock class="do-table">
        <button class="layui-btn layui-btn-danger" data-type="deleteAll"><i class="layui-icon"></i>批量删除</button>
        <button class="layui-btn" onclick="x_admin_show('创建合同','${pageContext.request.contextPath}/contract/add/page')">
            <i class="layui-icon"></i>创建
        </button>
    </xblock>
    <table id="data" lay-filter="contract"></table>


</div>
<script>
    var table;
    layui.use(['laydate', 'table'], function () {
        var laydate = layui.laydate;
        table = layui.table;

        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
            elem: '#end' //指定元素
        });

        table.render({
            elem: '#data'
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , height: 'full-200'
            , url: '${pageContext.request.contextPath}/contract/list/data' //数据接口
            , method: "post"
            , page: true //开启分页
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'contractId', title: 'ID', sort: true}
                , {field: 'contractName', title: '合同名称',}
                , {
                    field: 'contractType', title: '合同类型', templet: function (d) {
                        switch (d.contractType) {
                            case 0:
                                return '销售合同';
                                break;
                            case 1:
                                return '技术合同';
                                break;
                            case 2:
                                return '运输合同';
                                break;
                            case 3:
                                return '委托合同';
                                break;
                        }
                    }
                }
                // , {field: 'contractAmount', title: '合同金额'}
                , {
                    field: 'contractStatus', title: '状态', templet: function (d) {
                        switch (d.contractStatus) {
                            case 0:
                                return '已订立';
                                break;
                            case 1:
                                return '已提交';
                                break;
                            case 2:
                                return '待审核';
                                break;
                            case 3:
                                return '已审核';
                                break;
                            case 4:
                                return '履行中';
                                break;
                            case 5:
                                return '已履行';
                                break;
                            case 6:
                                return '已变更';
                                break;
                            case 7:
                                return '已解除';
                                break;
                            case 8:
                                return '已转让';
                                break;
                            case 9:
                                return '已终止';
                                break;
                            case 10:
                                return '已归档';
                                break;
                        }
                    }
                }
                // , {field: 'partyA', title: '合同甲方'}
                // , {field: 'partyB', title: '合同乙方'}
                // , {field: 'businessLicenseNoA', title: '甲方企业经营许可证号'}
                // , {field: 'businessLicenseNoB', title: '乙方企业经营许可证号'}
                // , {field: 'contractContents', title: '合同内容'}
                // , {field: 'contractRemarks', title: '合同备注'}
                // , {field: 'createTime', title: '创建时间', sort: true}
                , {
                    field: 'lastOperationTime', title: '最后操作时间', sort: true, templet: function (d) {
                        return d.lastOperationTime == null ? '未操作' : d.lastOperationTime;
                    }
                }
                // , {field: 'updateTime', title: '更新时间'}
                // , {field: 'createName', title: '创建人'}
                , {
                    field: 'operatorEmplId', title: '待处理人', templet: function (d) {
                        return d.empl.emplName;
                    }
                }
                , {fixed: 'right', title: '操作', width: '40%', align: 'center', toolbar: '#do'}
            ]]
        });


        //工具条事件
        table.on('tool(contract)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

            if (layEvent === 'see') { //查看
                x_admin_show('详情页面', '${pageContext.request.contextPath}/contract/queryDetail/page?id='+ data.contractId)
            } else if (layEvent === 'del') { //删除
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        type: "post",
                        url: "${pageContext.request.contextPath}/contract/deleteOne",
                        data: {id: data.contractId},
                        dataType: "json",
                        success: function (res) {
                            if (res.code === 200) {
                                layer.msg(res.msg, {icon: 6}, function () {
                                    // 刷新父级的表格
                                    table.reload('data');
                                });
                            }
                        }
                    });


                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);
                    //向服务端发送删除指令
                });
            } else if (layEvent === 'examination') { //审核
                x_admin_show('审核', '${pageContext.request.contextPath}/contract/editExamination/page?id=' + data.contractId)
            }else if (layEvent === 'change') { //更变
                x_admin_show('更变', '${pageContext.request.contextPath}/contract/editChange/page?id=' + data.contractId)
            }else if (layEvent === 'implement') { //执行
                x_admin_show('执行', '${pageContext.request.contextPath}/contract/editImplement/page?id=' + data.contractId)
            }else if (layEvent === 'exchange') { //转让
                x_admin_show('转让', '${pageContext.request.contextPath}/contract/editExchange/page?id=' + data.contractId)
            }else if (layEvent === 'relieve') { //解除
                x_admin_show('解除', '${pageContext.request.contextPath}/contract/editRelieve/page?id=' + data.contractId)
            } else if (layEvent === 'LAYTABLE_TIPS') {
                layer.alert('Hi，头部工具栏扩展的右侧图标。');
            }
        });


        var $ = layui.$, active = {
            deleteAll: function () {
                var checkStatus = table.checkStatus('data')
                    , data = checkStatus.data;
                if (data.length === 0) {
                    layer.msg('请至少选中一行数据，进行删除！');
                    return;
                }
                layer.confirm('确认要删除吗？', function (index) {
                    //获取选中数据中的id并组装成一个数组
                    var ids = [];
                    $(data).each(function () {
                        ids.push(this.contractId);
                    });
                    // 发送请求到后端
                    $.ajax({
                        type: "post",
                        url: "${pageContext.request.contextPath}/contract/delete",
                        data: {ids: ids},
                        dataType: "json",
                        success: function (res) {
                            layer.msg(res.msg, {icon: 6}, function () {
                                // 刷新父级的表格
                                table.reload('data');
                            });
                        }
                    });
                });
            }
        };

        $('.do-table .layui-btn-danger').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        $("#search").click(function () {
            //获取对应的数据
            // 获取关键字
            var contractName = $("input[name='contractName']").val();
            var contractType = $("select[name='contractType']").val();
            var contractStatus = $("select[name='contractStatus']").val();
            // 组装总共的提交参数
            var param = {
                contractName: contractName,
                contractType: contractType,
                contractStatus:contractStatus,
            };

            console.log(param);

            // 表格数据的重载
            table.reload("data", {where: param})
        });

    });


</script>


<script type="text/html" id="do">
    <a class="layui-btn layui-btn-xs" lay-event="see">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="change">变更</a>
    <a class="layui-btn layui-btn-xs" lay-event="implement">执行</a>
    <a class="layui-btn layui-btn-xs" lay-event="exchange">转让</a>
    <a class="layui-btn layui-btn-xs" lay-event="relieve">解除</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

</body>

</html>